<?php defined('SYSPATH') or die('No direct script access.');
                          
class Customer_Controller extends Template_Controller 
{
	public function __construct()
	{
		parent::__construct();

		$this->template->menu = '';
	}
	
	public function index($username)
	{
		$this->template->content = $username;
	}
		
	public function register()
	{
		$model = new Country_Model();
		$view = new View('customer/register');
		$view->countries = $model->find_all();
		$view->zones = Database::instance()->query('SELECT * FROM zones WHERE country_id=223');
		$view->returnurl = '/';
		$this->template->content =  $view->render();
	}

	public function create()
	{
		$model = new Customer_Model();
		
		$model->username = $_POST['username'];
		$model->password = base64_encode($_POST['password']);
		$model->firstname = $_POST['firstname'];
		$model->lastname = $_POST['lastname'];
		$model->email = $_POST['email'];
		$model->phone = $_POST['phone'];
		$model->fax = $_POST['fax'];
		$model->company = $_POST['company'];
		$model->created_at = date('Y-m-d H:i:s');
		$model->ip = myfuns::getip();
		
		if($model->save())
		{
			cookie::set('userid', $model->id);
			cookie::set('username', $model->username);
			cookie::set('firstname', $model->firstname);
			cookie::set('lastname', $model->lastname);
			
			url::redirect($_POST['returnurl'], 301);
			exit;
		}
	}
	
	public function login()
	{
		$this->template->content =  new View('customer/login', array('returnurl'=>$_SERVER['HTTP_REFERER']));
	}
	
	public function logout()
	{
		cookie::delete('userid');
		cookie::delete('username');
		cookie::delete('firstname');
		cookie::delete('lastname');
			
		url::redirect($_SERVER['HTTP_REFERER'], 301);
		exit;
	}
	
	public function checklogin()
	{
		$db = new Database();
		
		$result = $db->query("SELECT id, username, firstname, lastname FROM customers WHERE username='".$_POST['username']."' AND password='".base64_encode($_POST['password'])."'");

		foreach($result as $item)
		{
			cookie::set('userid', $item->id);
			cookie::set('username', $item->username);
			cookie::set('firstname', $item->firstname);
			cookie::set('lastname', $item->lastname);
		}
	
		$url = isset($_POST['returnurl']) ? $_POST['returnurl'] : $_SERVER['HTTP_REFERER'];
	
		url::redirect($url, 301);
		exit;
	}
	
	public function forgotpassword()
	{
		$view = new View('customer/forgotpassword');

		if( isset($_POST['userinfo']) )
		{
			$model = new Customer_Model();
			$result = $model->where('username', $_POST['userinfo'])->orwhere('email', $_POST['userinfo'])->find();
			
			if( $result->email != '' )
			{
				$config = config::item('email.options');
				
				$to      = array($result->email, $result->firstname.' '.$result->lastname);
				$from    = $config['username'];
				$subject = 'Send password';
				$message = '亲爱的用户：'.$result->firstname.' '.$result->lastname.'<br/><br/>';
				$message .= '您登录的密码是：'.base64_decode($result->password);
				 
				if(email::send($to, $from, $subject, $message, TRUE))
				{
					$view->errormsg = str_replace('%email%', $result->email, Kohana::lang('site.msg_forgot_password'));
				}
				else
				{
					$view->errormsg = str_replace('%email%', $result->email, Kohana::lang('site.msg_forgot_password_failed'));
				}
			}
			else
				$view->errormsg =  Kohana::lang('site.msg_forgot_password_error');
		}
		
		$this->template->content =  $view->render();
	}
}